package com.neuedu.yyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neuedu.yyzx.common.CommonResult;
import com.neuedu.yyzx.entity.Permission;
import com.neuedu.yyzx.entity.RolePermission;
import com.neuedu.yyzx.service.IPermissionService;
import com.neuedu.yyzx.service.IRolePermissionService;
import com.neuedu.yyzx.service.IRoleService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色权限 前端控制器
 * </p>
 *
 * @author dya
 * @since 2021-05-12
 */
@RestController
@RequestMapping("//rolePermission")
public class RolePermissionController {
    @Resource
    IRolePermissionService rolePermissionService;
    @Resource
    IPermissionService permissionService;


    @RequestMapping("/get-permiss")
    CommonResult getPermiss() {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",1111);
        wrapper.eq("active",1);
        List<Permission> list = permissionService.list(wrapper);
       /* for(Permission permission : list) {
            getChildren(permission);
        }*/

        return CommonResult.success(list);
    }

    @RequestMapping("/add")
    CommonResult add(Integer id,int[] permissionId ){
        String str="";

        if (permissionId.length>0){
            for (int i : permissionId) {

                str+=String.valueOf(i)+',';
            }
            str= str.substring(0, str.length() - 1);
        }else{
            str=null;
        }


        RolePermission rolePermission=new RolePermission();
        rolePermission.setRoleId(id);
        rolePermission.setPermissioinId(str);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("role_id",id);
        RolePermission rolePermission1=rolePermissionService.getOne(new QueryWrapper<RolePermission>().eq("role_id",id));
        if(rolePermission1!=null){
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.set("permissioin_id",str);
            updateWrapper.eq("role_id",id);
            return CommonResult.success(rolePermissionService.update(updateWrapper));
        }else {

            return CommonResult.success(  rolePermissionService.save(rolePermission));

        }


    }

    @RequestMapping("/getByRoleId")
    CommonResult getByRoleId(Integer id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("role_id",id);
        return CommonResult.success(rolePermissionService.getOne(queryWrapper));
    }


    /**
     * 参数为父权限对象
     * 通过父权限获取子权限
     *
     * */
    private void getChildren(Permission permission) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",permission.getId());
        wrapper.eq("active",1);
        List<Permission> list = permissionService.list(wrapper);
        for(Permission permission1 : list) {
//            if(permission.getType() != 0) {
            getChildren(permission1);
//            }
        }
        permission.setChildren(list);
    }



}
